package uit.com.project.iCampus.map;

import uit.com.project.iCampus.activity.R;
import uit.com.project.iCampus.application.ICampusApp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class CircleAreaOverlay extends Overlay{
	private Context context;
	private int radiusInMeter;

	public CircleAreaOverlay(Context context, int radiusInMeter){
		this.context = context;
		this.radiusInMeter = radiusInMeter;
	}
	
	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
		super.draw(canvas, mapView, shadow);
		drawMarker(mapView, ICampusApp.MY_LOCATION, canvas, R.drawable.my_location);
		drawCircle(mapView, canvas);
		return true;
	}
	
	public void drawCircle(MapView mapView, Canvas canvas){
		Paint paint = new Paint();
		paint.setColor(Color.BLUE);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setAlpha(15);
		paint.setStrokeWidth(5);
		
		Point screenPts = new Point();
		mapView.getProjection().toPixels(ICampusApp.MY_LOCATION, screenPts);
		float radius = mapView.getProjection().metersToEquatorPixels(radiusInMeter);
		
		canvas.drawCircle(screenPts.x, screenPts.y, radius, paint);
	}
	
	public void drawMarker(MapView mapView, GeoPoint geoPoint, Canvas canvas, int imageId){
		// ---translate the GeoPoint to screen pixels---
		Point screenPts = new Point();
		mapView.getProjection().toPixels(geoPoint, screenPts);
		// ---add the marker---
		Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), imageId);
		canvas.drawBitmap(bmp, screenPts.x - (bmp.getWidth() / 2), screenPts.y - (bmp.getHeight() / 2), null);
	}

}
